﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using Classes.Model.Dao;
using Classes.Model.Entidade;

namespace Juntae.Admin
{
    public partial class CadCategoria : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                this.BindData();
            }
        }

        private void BindData()
        {
            CategoriaDAO c = new CategoriaDAO();
            this.GridPanel1.Store.Primary.DataSource = c.select();
            this.GridPanel1.Store.Primary.DataBind();
        }

        protected void btnSalvar(object sender, DirectEventArgs e)
        {
            CategoriaDTO cat = new CategoriaDTO(lblDescricao.Text);
            CategoriaDAO c = new CategoriaDAO();
            c.inserir(cat);
            X.Msg.Alert("Salvando...", "Categoria Salva com Sucesso !").Show();
            BindData();
            lblDescricao.Text = "";
        }

        [DirectMethod(Namespace = "CompanyX")]
        public void AfterEdit(int id, string field, string oldValue, string newValue, object customer)
        {
            string message = "<b>Id:</b> {0}<br /><b>Campo:</b> {1}<br /><b>Valor Antigo:</b> {2}<br /><b>Novo Valor:</b> {3}";
            CategoriaDAO c = new CategoriaDAO();
            CategoriaDTO cat = new CategoriaDTO();
            cat.Descricao = newValue.ToString();
            cat.Id_categoria = id;

            c.atualizar(cat);
            // Send Message...
            X.Msg.Notify("Editando o registro #" + id.ToString(), string.Format(message, id, field, oldValue, newValue)).Show();

            this.GridPanel1.Store.Primary.CommitChanges();
        }

        protected void Command(object sender, DirectEventArgs e)
        {
            CategoriaDAO c = new CategoriaDAO();
            c.deletar(Convert.ToInt16(e.ExtraParams["id"]));
            BindData();
            X.Msg.Alert("Apagando", "Registro: " + e.ExtraParams["id"]).Show();

        }
    }
}